<template>
	<view class="create-order-container">
		<form @submit="handleSubmit">
			<view class="form-section">
				<view class="section-title">基本信息</view>
				
				<view class="form-item">
					<text class="label">需求标题 *</text>
					<input 
						class="input" 
						type="text" 
						placeholder="请输入需求标题" 
						v-model="form.title"
						maxlength="50"
					/>
				</view>
				
				<view class="form-item">
					<text class="label">技术分类 *</text>
					<picker 
						:range="categoryOptions" 
						range-key="name"
						:value="categoryIndex"
						@change="onCategoryChange"
					>
						<view class="picker">
							{{ categoryIndex >= 0 && categoryOptions[categoryIndex] ? categoryOptions[categoryIndex].name : '请选择技术分类' }}
						</view>
					</picker>
				</view>
				
				<view class="form-item">
					<text class="label">预算金额 *</text>
					<view class="input-with-unit">
						<input 
							class="input" 
							type="digit" 
							placeholder="请输入预算金额" 
							v-model="form.budget"
						/>
						<text class="unit">元</text>
					</view>
				</view>
				
				<view class="form-item">
					<text class="label">完成期限 *</text>
					<picker 
						mode="date" 
						:value="form.deadline"
						@change="onDateChange"
					>
						<view class="picker">
							{{ form.deadline || '请选择完成期限' }}
						</view>
					</picker>
				</view>
			</view>
			
			<view class="form-section">
				<view class="section-title">需求描述</view>
				
				<view class="form-item">
					<text class="label">详细描述 *</text>
					<textarea 
						class="textarea" 
						placeholder="请详细描述您的需求，包括功能要求、技术要求、交付标准等"
						v-model="form.description"
						maxlength="1000"
						show-confirm-bar="false"
					></textarea>
					<view class="char-count">{{ form.description.length }}/1000</view>
				</view>
			</view>
			
			<view class="form-section">
				<view class="section-title">联系方式</view>
				
				<view class="form-item">
					<text class="label">联系电话</text>
					<input 
						class="input" 
						type="number" 
						placeholder="请输入联系电话" 
						v-model="form.contactPhone"
					/>
				</view>
				
				<view class="form-item">
					<text class="label">QQ/微信</text>
					<input 
						class="input" 
						type="text" 
						placeholder="请输入QQ或微信号" 
						v-model="form.contactQQ"
					/>
				</view>
			</view>
			
			<view class="submit-section">
				<button 
					class="submit-btn" 
					:disabled="loading || !canSubmit"
					@click="handleSubmit"
				>
					{{ loading ? '发布中...' : '发布需求' }}
				</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					title: '',
					categoryId: '',
					budget: '',
					deadline: '',
					description: '',
					contactPhone: '',
					contactQQ: ''
				},
				categoryOptions: [],
				categoryIndex: -1,
				loading: false
			}
		},
		computed: {
			canSubmit() {
				return this.form.title.trim() && 
					   this.form.categoryId && 
					   this.form.budget && 
					   parseFloat(this.form.budget) > 0 && 
					   this.form.deadline && 
					   this.form.description.trim()
			}
		},
		onLoad() {
			this.loadCategories()
		},
		methods: {
			async loadCategories() {
				try {
					const res = await this.$api.getCategoryList()
					if (res.code === 200) {
						this.categoryOptions = res.data || []
					}
				} catch (error) {
					uni.showToast({
						title: '加载分类失败',
						icon: 'none'
					})
				}
			},
			
			onCategoryChange(e) {
				this.categoryIndex = e.detail.value
				this.form.categoryId = this.categoryOptions[this.categoryIndex].id
			},
			
			onDateChange(e) {
				this.form.deadline = e.detail.value
			},
			
			async handleSubmit() {
				if (!this.canSubmit) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				
				// 验证预算
				const budget = parseFloat(this.form.budget)
				if (isNaN(budget) || budget <= 0) {
					uni.showToast({
						title: '请输入正确的预算金额',
						icon: 'none'
					})
					return
				}
				
				// 验证日期
				const deadline = new Date(this.form.deadline)
				const now = new Date()
				if (deadline <= now) {
					uni.showToast({
						title: '完成期限必须大于当前时间',
						icon: 'none'
					})
					return
				}
				
				this.loading = true
				
				try {
					const submitData = {
						title: this.form.title.trim(),
						categoryId: this.form.categoryId,
						budget: budget,
						deadline: this.form.deadline + 'T23:59:59',
						description: this.form.description.trim(),
						contactPhone: this.form.contactPhone.trim(),
						contactQQ: this.form.contactQQ.trim()
					}
					
					await this.$api.createOrder(submitData)
					
					uni.showToast({
						title: '发布成功',
						icon: 'success'
					})
					
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
					
				} catch (error) {
					uni.showToast({
						title: error.message || '发布失败',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.create-order-container {
		background: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx;
	}
	
	.form-section {
		background: #fff;
		border-radius: 15rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		
		.section-title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 30rpx;
			padding-bottom: 20rpx;
			border-bottom: 2rpx solid #f0f0f0;
		}
		
		.form-item {
			margin-bottom: 40rpx;
			position: relative;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.label {
				display: block;
				font-size: 28rpx;
				color: #333;
				margin-bottom: 20rpx;
				font-weight: 500;
			}
			
			.input, .picker {
				width: 100%;
				height: 80rpx;
				border: 2rpx solid #e0e0e0;
				border-radius: 10rpx;
				padding: 0 30rpx;
				font-size: 28rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				background: #fff;
				
				&:focus {
					border-color: #007AFF;
				}
			}
			
			.picker {
				color: #333;
				
				&:empty::before {
					content: attr(placeholder);
					color: #999;
				}
			}
			
			.textarea {
				width: 100%;
				min-height: 200rpx;
				border: 2rpx solid #e0e0e0;
				border-radius: 10rpx;
				padding: 30rpx;
				font-size: 28rpx;
				box-sizing: border-box;
				line-height: 1.5;
				
				&:focus {
					border-color: #007AFF;
				}
			}
			
			.char-count {
				text-align: right;
				font-size: 24rpx;
				color: #999;
				margin-top: 10rpx;
			}
			
			.input-with-unit {
				position: relative;
				display: flex;
				align-items: center;
				
				.input {
					flex: 1;
					padding-right: 80rpx;
				}
				
				.unit {
					position: absolute;
					right: 30rpx;
					font-size: 28rpx;
					color: #666;
				}
			}
		}
	}
	
	.submit-section {
		padding: 40rpx 0;
		
		.submit-btn {
			width: 100%;
			height: 88rpx;
			background: linear-gradient(45deg, #007AFF, #0056CC);
			color: #fff;
			border: none;
			border-radius: 44rpx;
			font-size: 32rpx;
			font-weight: bold;
			
			&:disabled {
				opacity: 0.6;
				background: #ccc;
			}
		}
	}
</style>